package com.letter.otools.day.admob;

import android.os.CountDownTimer;

import androidx.annotation.NonNull;

import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.appopen.AppOpenAd;
import com.letter.otools.day.BaseActivity;

import java.util.concurrent.atomic.AtomicBoolean;

public abstract class AdmobSplashActivity extends BaseActivity {
    private static final long COUNTER_TIME_MILLISECONDS = 3000;
    private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
    private AppOpenAdManager appOpenAdManager;
    private CountDownTimer countDownTimer;

    @Override
    protected void initView() {
        appOpenAdManager = AdmobUtils.appOpenAdManager;
        createTimer(COUNTER_TIME_MILLISECONDS);
        if (appOpenAdManager.canRequestAds()) {
            initializeMobileAdsSdk();
        }
    }

    /**
     * Create the countdown timer, which counts down to zero and show the app open ad.
     *
     * @param time the number of milliseconds that the timer counts down from
     */
    private void createTimer(long time) {
        countDownTimer = new CountDownTimer(time, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
//                counterTextView.setText("App is done loading in: " + secondsRemaining);
            }

            @Override
            public void onFinish() {
//                counterTextView.setText("Done.");
                goMain();
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    private void initializeMobileAdsSdk() {
        if (isMobileAdsInitializeCalled.getAndSet(true)) {
            return;
        }

        // Initialize the Mobile Ads SDK.
        MobileAds.initialize(this);

        // Load an ad.
        if (appOpenAdManager.isAdAvailable()) {
            appOpenAdManager.showAdIfAvailable(this, new OnShowAdCompleteListener() {
                @Override
                public void onAdShowedFullScreenContent() {
                    if (countDownTimer != null) {
                        countDownTimer.cancel();
                    }
                }

                @Override
                public void onShowAdComplete() {
                    goMain();
                }
            });
        } else {
            appOpenAdManager.loadAd(this, new AppOpenAd.AppOpenAdLoadCallback() {
                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    super.onAdFailedToLoad(loadAdError);
//                    goMain();
                }

                @Override
                public void onAdLoaded(@NonNull AppOpenAd appOpenAd) {
                    super.onAdLoaded(appOpenAd);
                    appOpenAdManager.showAdIfAvailable(getActivity(), new OnShowAdCompleteListener() {
                        @Override
                        public void onAdShowedFullScreenContent() {
                            if (countDownTimer != null) {
                                countDownTimer.cancel();
                            }
                        }

                        @Override
                        public void onShowAdComplete() {
                            goMain();
                        }
                    });
                }
            });
        }
    }

    protected abstract void goMain();
}
